package com.handydata.antifraud.ws.redis;

import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.service.DictInfoService;
import com.handydata.antifraud.to.dap.DictTansResTO;
import com.handydata.antifraud.to.dap.DictTransReqTO;
import com.handydata.antifraud.ws.AbstractWsService;
import com.handydata.antifraud.ws.WsInvocationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


/**
 * 统计上行下发的数据，返回给规则引擎用
 */
@Service("phoneCityTransService")
public class PhoneCityTransImpl extends AbstractWsService<DictTransReqTO, DictTansResTO>
	implements WsInvocationService<DictTransReqTO, DictTansResTO> {

	private static final Logger logger = LoggerFactory.getLogger(PhoneCityTransImpl.class);

	@Autowired
	private DictInfoService dictInfoService;
	/**
	 * @see AbstractWsService#register()
	 */
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, DictTransReqTO.class);
	}

	@Override
	public DictTansResTO query(DictTransReqTO req) throws WsInvokeException {

		DictTansResTO res=new DictTansResTO();
		try{

			if(req.getPhone()!=null&&req.getPhone().length()>=7){

				Object obj=dictInfoService.getDictInfo(req.getPhone().substring(0,7),DictInfoCode.DICT_PHONE_CITY.getCode());
				if(obj!=null){
					res.setPhoneCity(obj.toString());
				}

			}
			if(req.getRelationPhone()!=null&&req.getRelationPhone().length()>=7){

				Object obj=dictInfoService.getDictInfo(req.getRelationPhone().substring(0,7),DictInfoCode.DICT_PHONE_CITY.getCode());
				if(obj!=null){
					res.setRelationCity(obj.toString());
				}

			}
			res.setResCode("0000");
			res.setResMsg("翻译手机号码所属地成功");
			return res;
		}catch (Exception ex){
			logger.error("统计上行数据出错{}",req,ex);//该抛异常还是返回错误代码？
		}
		return res;
	}
}
